package com.anrongtec.zcpt.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.anrongtec.zcpt.ApiConfig;
import com.anrongtec.zcpt.R;
import com.anrongtec.zcpt.adapter.common.CommonAdapter;
import com.anrongtec.zcpt.adapter.common.ViewHolder;
import com.anrongtec.zcpt.base.BaseActivity;
import com.anrongtec.zcpt.model.AppModel;
import com.anrongtec.zcpt.utils.ApkUtils;
import com.anrongtec.zcpt.widget.EmptyWrapper;

import java.util.ArrayList;
import java.util.List;

public class DownloadManagerActivity extends BaseActivity {

    private RecyclerView rl_manager_all_app;

    private SwipeRefreshLayout swipe_refresh;
    private CommonAdapter<AppModel> adapter;
    private List<AppModel> apps, newApps;

    private EmptyWrapper empty;

    private static final int SPAN_COUNT = 4;

    public static final int APP_REFRESH = 1;


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case APP_REFRESH:
                    empty.setEmptyView(R.layout.empty_view);
                    rl_manager_all_app.setAdapter(empty);
//                    apps.clear();
//                    apps.addAll(newApps);
                    empty.notifyDataSetChanged();
                    if (swipe_refresh != null && swipe_refresh.isRefreshing()) {
                        swipe_refresh.setRefreshing(false);
                    }
                    break;

                default:
                    break;
            }

//            tv_num.setText("我的应用共"+apps.size()+"款");

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleShow(false);
        setContentView(R.layout.activity_download_manager);
        initTopBar(this,true,"下载管理","");
        initData();
        initView();
    }


    /**
     * 初始化数据
     */
    protected void initData() {
        List<AppModel> appModels = ApkUtils.scanLocalInstallAppList(getPackageManager());
        apps = new ArrayList<>();
        apps.addAll(appModels);
        newApps = new ArrayList<>();
    }

    private void initView() {
        rl_manager_all_app = (RecyclerView)findViewById(R.id.rl_manager_all_app);
        initSwipeRefresh();
        initRecyclerView();
    }

    /**
     * 初始化recyclerview
     *
     */
    private void initRecyclerView() {
        rl_manager_all_app.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        adapter = new CommonAdapter<AppModel>(this, R.layout.item_shopping_all_app, apps) {

            @Override
            protected void convert(ViewHolder holder, AppModel t, int position) {
                holder.setImageDrawable(R.id.iv_item_shopping_all_app, t.getLocalIcon());
                holder.setText(R.id.tv_item_shopping_all_app_title, t.getAppName());
                holder.setText(R.id.tv_item_shopping_all_app_size, String.valueOf(t.getSize() / (1000 * 1000)) + "MB");
                holder.setText(R.id.tv_item_shopping_all_app_content, t.getDescription());
                final AppModel model = t;
                holder.setOnClickListener(R.id.btn_item_shopping_all_app_install, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.e(TAG,model.getAppPackName()+""+model.getSize());
                        ApkUtils.uninstall(DownloadManagerActivity.this,model.getAppPackName());
                    }
                });
            }

        };

//        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(View view,
//                                    RecyclerView.ViewHolder holder,
//                                    int position) {
//                ApkUtils.openAPP(DownloadManagerActivity.this, apps.get(position).getAppPackName());
//            }
//        });

        empty = new EmptyWrapper(adapter);
        empty.setEmptyView(R.layout.loading_view);


        rl_manager_all_app.setAdapter(empty);
    }

    /**
     * 初始化下拉刷新
     *
     */
    private void initSwipeRefresh() {
        swipe_refresh = (SwipeRefreshLayout)findViewById(R.id.sr_manager_all_app);
        swipe_refresh.setEnabled(false);
        swipe_refresh.setColorSchemeResources(
                android.R.color.holo_blue_bright,
                android.R.color.holo_orange_light,
                android.R.color.holo_green_light);
        swipe_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                swipe_refresh.setRefreshing(true);
                getAppsInfo();
            }


        });
    }

    //获取数据
    private void getAppsInfo() {
        handler.sendEmptyMessageDelayed(APP_REFRESH, ApiConfig.DELAY_TIME);
    }


}
